
extern crate winit;
extern crate vulkano as vk;
extern crate vulkano_win;

use winit::{ dpi::PhysicalSize};
use winit::{
    event::{Event, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    window::WindowBuilder,
};

use std::sync::Arc;

use vulkano::instance:: {
    Instance,
    InstanceExtensions,
    ApplicationInfo,
    Version,
};

const WIDTH: u32 = 1024;
const HEIGHT: u32 = 768;


struct Application {
    mInstance: Arc<Instance>,
}

impl Application {
    pub fn new() -> Self {
        let inst = Self::create_instance();
        Self {
            mInstance:inst,
        }
    }

    fn create_instance() -> Arc<Instance> {
        let supported_extensions = InstanceExtensions::supported_by_core()
            .expect("获取vulkan扩展失败！");
        println!("扩展有：{:?}", supported_extensions);
        
        let app_info = ApplicationInfo {
            application_name: Some("Hello vulkan".into()),
            application_version: Some(Version{major: 1, minor: 0, patch: 0}),
            engine_name: Some("DOO3D".into()),
            engine_version:Some(Version{major:1, minor:0, patch: 0}),
        };

        let required_extensions = vulkano_win::required_extensions();

        Instance::new(Some(&app_info),Version::V1_1, &required_extensions, None)
            .expect("创建实例失败")
    }

    pub fn run(&mut self) {
        let event_loop = EventLoop::new();
        let _window = WindowBuilder::new()
            .with_title("第01 创建实例")
            // .with_resizable(false) // false：窗口不可缩放
            .with_inner_size(PhysicalSize::new(WIDTH, HEIGHT))
            .build(&event_loop)
            .unwrap();

        event_loop.run(move |event, _, control_flow |{
            *control_flow = ControlFlow::Poll;
            *control_flow = ControlFlow::Wait;

            match event {
                Event::WindowEvent {
                    event: WindowEvent::CloseRequested,
                    ..
                } => {
                    println!("窗口关闭！");
                    *control_flow = ControlFlow::Exit;
                },
                Event::WindowEvent {
                    event: WindowEvent::Resized(_),
                    ..
                } => {
                    println!("window resize");
                },
                Event::MainEventsCleared => {
                    // Application update code.

                    // Queue a RedrawRequested event.
                    //
                    // You only need to call this if you've determined that you need to redraw, in
                    // applications which do not always need to. Applications that redraw continuously
                    // can just render here instead.

                    _window.request_redraw();
                },
                Event::RedrawRequested(_) => {
                    // Redraw the application.
                    //
                    // It's preferable for applications that do not render continuously to render in
                    // this event rather than in MainEventsCleared, since rendering in here allows
                    // the program to gracefully handle redraws requested by the OS.
                },
                _ =>()
            }
        });
    }
}

fn main() {
    let mut app = Application::new();
    app.run();
}